<template>
  <!-- 添加或修改费用配置对话框 -->
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="1000px"
    append-to-body
    :close-on-click-modal="false"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="98px" label-position="right" class="flex-form"
             size="small">
      <el-form-item label="场所代码" prop="fieldCode">
        <el-select v-model.trim="form.fieldCode" placeholder="请选择场所代码">
          <el-option v-for="item in fieldList" :key="item.fieldCode" :label="item.fieldCode+'-'+item.fieldName" :value="item.fieldCode" @click.native="setFiledName(item)"/>
        </el-select>
      </el-form-item>

      <el-form-item label="进出口类型" prop="ieFlag">
        <el-select v-model.trim="form.ieFlag" placeholder="请选择进出口类型">
          <el-option
            v-for="dict in dict.type.sys_i_e_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="车型" prop="veType">
        <el-select v-model.trim="form.veType" placeholder="请选择车型">
          <el-option
            v-for="dict in dict.type.fee_vehicle_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="业务类型" prop="businessType">
        <el-select v-model.trim="form.businessType" placeholder="请选择业务类型">
          <el-option
            v-for="dict in dict.type.estimate_businuss_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="费用类型" prop="feeType">
        <el-select v-model.trim="form.feeType" placeholder="请选择费用类型">
          <el-option
            v-for="dict in dict.type.charge_charge_Type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="收费单价" prop="chargePrice">
        <el-input v-model.trim="form.chargePrice" type="number" placeholder="请输入收费单价"/>
      </el-form-item>

      <el-form-item label="计费单位" prop="chargeUnit">
        <el-input v-model.trim="form.chargeUnit" placeholder="请输入计费单位"/>
      </el-form-item>

      <el-form-item label="启停用状态">
        <el-radio-group v-model.trim="form.status">
          <el-radio
            v-for="dict in dict.type.start_stop_status"
            :key="dict.value"
            :label="dict.value"
          >{{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getFeeConfig, addFeeConfig, updateFeeConfig} from "@/api/miniapp/FeeConfig";

export default {
  name: "FeeConfig-addEdit",
  props: {
    dialogType: {
      type: String,
      default: "add",
    },
    rowData: {
      type: Object,
      default: () => {
      },
    },
    dict: {}
  },
  data() {
    return {
      fieldList: [],
      // 弹出层
      dialogVisible: false,
      // 标题
      title: "新增",
      // 表单参数
      form: {
        id: null,
        fieldCode: null,
        fieldName: null,
        ieFlag: null,
        veType: null,
        businessType: null,
        feeType: null,
        chargePrice: null,
        chargeUnit: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null

      },
      // 费用配置表格数据
      FeeConfigList: [],
      // 表单校验
      rules: {
        fieldCode: [
          {required: true, message: "场所代码不能为空", trigger: "change"}
        ],
        ieFlag: [
          {required: true, message: "进出口类型不能为空", trigger: "change"}
        ],
        veType: [
          {required: true, message: "车型不能为空", trigger: "change"}
        ],
        feeType: [
          {required: true, message: "费用类型不能为空", trigger: "change"}
        ],
        chargePrice: [
          {required: true, message: "收费单价不能为空", trigger: "blur"}
        ],
      }
    };
  },
  methods: {
    open(fieldList) {
      this.fieldList=fieldList;
      this.dialogVisible = true;
      this.$nextTick(() => {
        if (this.dialogType == "add") {
          this.title = "新增";
        } else if (this.dialogType == "edit") {
          this.title = "编辑";
          this.loadDetail();
        } else if (this.dialogType == "detail") {
          this.title = "详情";
          this.loadDetail();
        }
      });
    },
    close() {
      this.$refs["form"].resetFields();
      this.dialogVisible = false;
    },

    // 获取详情
    loadDetail() {
      const id = this.rowData.id;
      getFeeConfig(id).then((res) => {
        if (res.code == 200) {
          this.form = res.data;
        }
      });
    },

    // 提交表单
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let params = {
            ...this.form,
          };
          if (this.dialogType == "add") {
            addFeeConfig(this.form).then(res => {
              if (res.code === 200) {
                this.$modal.msgSuccess("新增成功");
                this.close();
                this.$emit("handleQuery");
              }
            });
          } else {
            params.id = this.rowData.id;
            updateFeeConfig(this.form).then(res => {
              if (res.code === 200) {
                this.$modal.msgSuccess("修改成功");
                this.close();
                this.$emit("handleQuery");
              }
            });
          }
        }
      });
    },
    setFiledName(opiton){
      console.log("场所名称："+opiton.fieldName)
      this.form.fieldName=opiton.fieldName;
    }
  },
};
</script>

<style lang="scss" scoped></style>
